package com.exporter.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.exporter.exception.ExporterException;
import com.exporter.utility.ConfigurationLoader;
import com.exporter.utility.ExporterUtility;

public class ExportFilesActionListener implements ActionListener
{

    JTextField txtSrcFolder;
    JTextField txtDestFolder;
    JTextArea txtarFolderIgnore;
    JTextArea txtarFileIgnore;
    JCheckBox chkbxFolderStructure;
    JCheckBox chkbxZipFile;
    JCheckBox chkbxSftp;
    JTextField txtSftpHost;
    JTextField txtSftpPort;
    JTextField txtUserName;
    JPasswordField txtPassword;
    JTextField txtUploadPath;
    JFrame frame;
    JCheckBox chkbxEnableAdvOptions;
    JRadioButton radioExportFromDate;
    JRadioButton radioExportFilesOfDate;
    JComboBox comboExportFromDate;
    JComboBox comboToDate;
    JComboBox comboFileOfDate;
    JCheckBox chkbxLogFile;

    private boolean copyFlag = true;
    private boolean zipFlag = true;
    private boolean sftpFlag = true;

    public ExportFilesActionListener(JTextField txtSrcFolder, JTextField txtDestFolder, JTextArea txtarFolderIgnore, JTextArea txtarFileIgnore, JCheckBox chkbxFolderStructure, JCheckBox chkbxZipFile,
            JCheckBox chkbxSftp, JTextField txtSftpHost, JTextField txtSftpPort, JTextField txtUserName, JPasswordField txtPassword, JTextField txtUploadPath, JFrame frame, JCheckBox chkbxEnableAdvOptions,
            JRadioButton radioExportFromDate, JRadioButton radioExportFilesOfDate, JComboBox comboExportFromDate, JComboBox comboToDate, JComboBox comboFileOfDate, JCheckBox chkbxLogFile)
    {
        this.txtSrcFolder = txtSrcFolder;
        this.txtDestFolder = txtDestFolder;
        this.txtarFolderIgnore = txtarFolderIgnore;
        this.txtarFileIgnore = txtarFileIgnore;
        this.chkbxFolderStructure = chkbxFolderStructure;
        this.chkbxZipFile = chkbxZipFile;
        this.chkbxSftp = chkbxSftp;
        this.txtSftpHost = txtSftpHost;
        this.txtSftpPort = txtSftpPort;
        this.txtUserName = txtUserName;
        this.txtPassword = txtPassword;
        this.txtUploadPath = txtUploadPath;
        this.frame = frame;
        this.chkbxEnableAdvOptions = chkbxEnableAdvOptions;
        this.radioExportFromDate = radioExportFromDate;
        this.radioExportFilesOfDate = radioExportFilesOfDate;
        this.comboExportFromDate = comboExportFromDate;
        this.comboToDate = comboToDate;
        this.comboFileOfDate = comboFileOfDate;
        this.chkbxLogFile = chkbxLogFile;
    }

    @Override
    public void actionPerformed(ActionEvent ae)
    {  	
        boolean result = validateInputs();
        Properties properties = new Properties();
        if ( result )
        {
            if ( chkbxEnableAdvOptions.isSelected() )
            {
                try
                {
                    properties.setProperty( "folder.ignore", txtarFolderIgnore.getText() );
                    properties.setProperty( "file.ignore", txtarFileIgnore.getText() );
                    if ( radioExportFromDate.isSelected() )
                    {
                        copyFlag = ExporterUtility.exportFilesInDateRange( (String) comboExportFromDate.getSelectedItem(), (String) comboToDate.getSelectedItem(), txtSrcFolder.getText(), txtDestFolder.getText(),
                                txtarFileIgnore.getText(), txtarFolderIgnore.getText(), chkbxFolderStructure.isSelected(), chkbxLogFile.isSelected() );
                    }
                    else if ( radioExportFilesOfDate.isSelected() )
                    {
                        copyFlag = ExporterUtility.exportFilesOfSpecificDate((String)comboFileOfDate.getSelectedItem(), txtSrcFolder.getText(), txtDestFolder.getText(),
                                txtarFileIgnore.getText(), txtarFolderIgnore.getText(), chkbxFolderStructure.isSelected(), chkbxLogFile.isSelected());
                    }
                }
                catch ( ExporterException eex )
                {
                    JOptionPane.showMessageDialog( frame, eex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
                    return;
                }
            }
            else
            {
                try
                {
                    properties.setProperty( "folder.ignore", txtarFolderIgnore.getText() );
                    properties.setProperty( "file.ignore", txtarFileIgnore.getText() );
                    ExporterUtility.modificationDetail.clear();
                    boolean importedOnToday = ExporterUtility.analyzeWorkspace( txtSrcFolder.getText(), txtarFolderIgnore.getText(), txtarFileIgnore.getText() );
                    if ( importedOnToday )
                    {
                        ExporterUtility.getRecursiveListing( txtSrcFolder.getText(), txtarFolderIgnore.getText(), txtarFileIgnore.getText(), "searchOnTime" );
                    }
                    else
                    {
                        ExporterUtility.getRecursiveListing( txtSrcFolder.getText(), txtarFolderIgnore.getText(), txtarFileIgnore.getText(), "searchOnDate" );
                    }
                    copyFlag = ExporterUtility.copyModifiedFiles( txtSrcFolder.getText(), txtDestFolder.getText(), chkbxFolderStructure.isSelected(), chkbxLogFile.isSelected() );
                }
                catch ( ExporterException eex )
                {
                    JOptionPane.showMessageDialog( frame, eex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
                    return;
                }
            }
            if ( chkbxZipFile.isSelected() )
            {
                try
                {
                    zipFlag = ExporterUtility.createZipFile( txtDestFolder.getText() );
                }
                catch ( ExporterException eex )
                {
                    JOptionPane.showMessageDialog( frame, eex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
                    return;
                }
            }

            if ( chkbxSftp.isSelected() )
            {
                List< String > configParams = new ArrayList< String >();
                configParams.add( txtSftpHost.getText() );
                configParams.add( txtSftpPort.getText() );
                configParams.add( txtUserName.getText() );
                configParams.add( new String( txtPassword.getPassword() ) );
                configParams.add( txtUploadPath.getText() );
                configParams.add( txtDestFolder.getText() );
                properties.setProperty( "host.ip", txtSftpHost.getText() );
                properties.setProperty( "host.port", txtSftpPort.getText() );
                properties.setProperty( "host.user", txtUserName.getText() );
                properties.setProperty( "host.password", new String( txtPassword.getPassword() ) );
                properties.setProperty( "file.upload.path", txtUploadPath.getText() );
                try
                {
                    sftpFlag = ExporterUtility.uploadToSftp( configParams );
                }
                catch ( ExporterException eex )
                {
                    JOptionPane.showMessageDialog( frame, eex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
                    return;
                }
            }
            else
            {
                properties.setProperty( "host.ip", ConfigurationLoader.HOST_IP );
                properties.setProperty( "host.port", ConfigurationLoader.HOST_PORT );
                properties.setProperty( "host.user", ConfigurationLoader.HOST_USER );
                properties.setProperty( "host.password", ConfigurationLoader.HOST_PASSWORD );
                properties.setProperty( "file.upload.path", ConfigurationLoader.FOLDER_UPLOAD_PATH );
            }
            ConfigurationLoader.storeConfiguration( properties );
            if ( copyFlag && zipFlag && sftpFlag )
            {
                JOptionPane.showMessageDialog( frame, "File(s) have been exported.", "Done", JOptionPane.INFORMATION_MESSAGE );
            }
            else
            {
                JOptionPane.showMessageDialog( frame, "No file to export.", "Done", JOptionPane.INFORMATION_MESSAGE );
            }
//            waitDialog.dispose();
        }
        else
        {
//        	waitDialog.dispose();
            return;
        }
    }

    private boolean validateInputs()
    {
        boolean flag = true;
        if ( txtSrcFolder.getText().equals( "" ) )
        {
            JOptionPane.showMessageDialog( frame, "Source folder cannot be empty !", "Empty filed", JOptionPane.ERROR_MESSAGE );
            flag = false;
        }
        else if ( txtDestFolder.getText().equals( "" ) )
        {
            JOptionPane.showMessageDialog( frame, "Destination folder cannot be empty !", "Empty filed", JOptionPane.ERROR_MESSAGE );
            flag = false;
        }
        else if ( chkbxSftp.isSelected() && txtSftpHost.getText().equals( "" ) )
        {
            JOptionPane.showMessageDialog( frame, "Host cannot be empty !", "Empty filed", JOptionPane.ERROR_MESSAGE );
            flag = false;
        }
        else if ( chkbxSftp.isSelected() && txtSftpPort.getText().equals( "" ) )
        {
            JOptionPane.showMessageDialog( frame, "Port cannot be empty !", "Empty filed", JOptionPane.ERROR_MESSAGE );
            flag = false;
        }
        else if ( chkbxSftp.isSelected() && txtUserName.getText().equals( "" ) )
        {
            JOptionPane.showMessageDialog( frame, "Username cannot be empty !", "Empty filed", JOptionPane.ERROR_MESSAGE );
            flag = false;
        }
        else if ( chkbxSftp.isSelected() && txtPassword.getPassword().toString().equals( "" ) )
        {
            JOptionPane.showMessageDialog( frame, "Password cannot be empty !", "Empty filed", JOptionPane.ERROR_MESSAGE );
            flag = false;
        }
        else if ( chkbxSftp.isSelected() && txtUploadPath.getText().equals( "" ) )
        {
            JOptionPane.showMessageDialog( frame, "Please specify a path for uploading !", "Empty filed", JOptionPane.ERROR_MESSAGE );
            flag = false;
        }

        return flag;
    }

}
